package com.match.utils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.match.model.KeyGradeModel;

public class TxtUtils {

    /**
     * 以FileWriter方式写入txt文件。
     * @param File file：要写入的文件
     * @param String content： 要写入的内容
     * @param String charset:要写入内容的编码方式
     */
    public static void writeToFile(List<String> result, String filePath) {

        try {
            File file = new File(filePath);
            if (file.exists()) {
                FileWriter fw = new FileWriter(file, true);// 追加形式添加
                BufferedWriter bw = new BufferedWriter(fw);
                for (int i = 0; i < result.size(); i++) {
                    bw.write(result.get(i) + ",");
                }
                bw.close();
                fw.close();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 将结果集合放入txt
     */
    public static void writeResultToFile(List<String> result, String filePath) {

        try {
            File file = new File(filePath);
            if (file.exists()) {
                FileWriter fw = new FileWriter(file, true);// 追加形式添加
                BufferedWriter bw = new BufferedWriter(fw);
                for (int i = 0; i < result.size(); i++) {
                    bw.write(result.get(i) + "\n");
                }
                bw.close();
                fw.close();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * @Description 获取一段话中关键字在一个Text文件中出现的概率(无权重)及次数
     * @author xd 2018年1月26日 下午3:17:28
     */
    @SuppressWarnings("resource")
    public static List<Object> getKeyListProbabilityOfTypeTxt(List<KeyGradeModel> keyList,
            String filePath) {
        List<Object> result = new ArrayList<>();
        try {
            File file = new File(filePath);
            BufferedReader bf = new BufferedReader(new FileReader(file));
            String str=bf.readLine();
            String[] s = null;
            if (null != str) {
                if (!str.trim().isEmpty()) {
                    s = str.split(",");
//                    System.out.println(s[0]);
                }
            }
            List<String> keySetList = Arrays.asList(s);
            Integer number = 0;// 总出现次数
            BigDecimal score = new BigDecimal(0);
            for (int i = 0; i < keyList.size(); i++) {
                int numberEach = 0;
                for (int j = 0; j < keySetList.size(); j++) {
                    if (keyList.get(i).getKeyName().equals(keySetList.get(j))) {
                        number++;
                        numberEach++;
                    }
                }
                // 所有关键字的总分
                score = score.add(keyList.get(i).getKeyWeight().abs().multiply(new BigDecimal(numberEach)));
            }

            result.add(number);
            result.add(score);
//            System.out.println(number);
//            System.out.println(score);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}
